package com.olc.railing.utils;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
    //时间转毫秒
    public static  Long getMilliSecond(String format, String values) {
        Long time = 0L;
        //得到毫秒数
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            time = sdf.parse(values).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            MyLog.e("转毫秒", "-----异常---");
        }
        return time;
    }



    /**
     * 当前时间是否在可用范围
     * @param startHour
     * @param endHour
     * @return
     */
    public static boolean isAround(int startHour,int  endHour){
        Calendar ca = Calendar.getInstance();
        int hour = ca.get(Calendar.HOUR_OF_DAY);   // 获取日 6 19
        MyLog.d(MyLog.TAG,"hour: "+hour);
        return hour>startHour&&hour<endHour;
    }

    //毫秒转时间
    public static String getTime(String pattern, String dateTime) {
        String timestr="";
        try {
            Long time = Long.parseLong(dateTime);
            SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
            timestr=sDateFormat.format(new Date(time + 0));
        }catch (Exception e) {
            e.printStackTrace();
            MyLog.e("转毫秒", "-----异常---");
        }

        return timestr;
    }
    public static String getLastMonth(String payoffYearMonth) {
        SimpleDateFormat  sd=new    SimpleDateFormat("yyyy-MM");

        try {
            Date  currdate = sd.parse(payoffYearMonth);
            Calendar calendar= Calendar.getInstance();
            calendar.setTime(currdate);
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
            return sd.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static long getPreviousTenYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -10);
        return c.getTime().getTime();
    }

    public static long getTenYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, 10);
        return c.getTime().getTime();
    }

    public static long getStartTime() {
        long currentTime = System.currentTimeMillis();
        long finalTime = getMilliSecond("yyyy-MM-dd","2020-01-01");
        if(currentTime<finalTime){
               return finalTime;
        }
        return currentTime;
    }

    public static long getEndTime(long startTimeMillis) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(startTimeMillis);
        c.add(Calendar.YEAR, 10);
        return c.getTime().getTime();

    }

    public static long getLastMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        return c.getTime().getTime();
    }
    public static long getFourthMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 14);
        return c.getTime().getTime();
    }

    public static long getPreviousThreeYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -3);
        return c.getTime().getTime();
    }
    public static String getTimeDeleteT(String time) {
        String result = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = simpleDateFormat.parse(time);
            result =  getTime("yyyy-MM-dd HH:mm:ss",date.getTime()+"");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static int lengthOfLongestSubstring(String s) {
        int frist = 0;
        String result = "";
        for(int i= 0 ;i<s.length();i++){
            char var = s.charAt(i);
            if(result.indexOf(var)!=-1){
                result = result.substring(result.indexOf(var)+1)+var;
            }else{
                result += var;
            }
            if(result.length()>frist){
                frist = result.length();
            }
        }
        return frist;

    }

}
